/**
 * 获取当前页url
 */
function getCurrentPageUrl() {
  var pages = getCurrentPages()    //获取加载的页面
  var currentPage = pages[pages.length - 1]    //获取当前页面的对象
  var url = currentPage.route    //当前页面url
  return url
}
/**
 * 获取当前页对象
 */
function getCurrentPageObj(index) {
  if (!index) {
    index = 1;
  }
  var pages = getCurrentPages()    //获取加载的页面
  var currentPage = pages[pages.length - index]    //获取当前页面的对象
  return currentPage
}
/**
 * 获取当前页带参数的url
 */
function getCurrentPageUrlWithArgs() {
  var pages = getCurrentPages()    //获取加载的页面
  var currentPage = pages[pages.length - 1]    //获取当前页面的对象
  var url = currentPage.route    //当前页面url
  var options = currentPage.options    //如果要获取url中所带的参数可以查看options
  //拼接url的参数
  var urlWithArgs = url + '?'
  for (var key in options) {
    var value = options[key]
    urlWithArgs += key + '=' + value + '&'
  }
  urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
  return urlWithArgs
}
/**
 * 提示
 */
function msg(str, time) {
  if (!time) {
    time = 1000;
  }
  wx.showToast({
    title: str,
    icon: 'none',
    duration: time,
    mask: true
  });
}
/**
 * 跳转普通页面
 */
function goUrl(url) {
  wx.navigateTo({
    url: url
  })
}
/**
 * 跳转tab页面
 */
function goUrlTab(url) {
  wx.switchTab({
    url: url
  })
}
/**
 * 跳转普通页面-关闭当前
 */
function goUrlClose(url) {
  wx.redirectTo({
    url: url
  })
}
/**
 * 成功提示
 */
function success(str, time, success_callback) {
  if (!time) {
    time = 1000;
  }
  if (!success_callback) {
    success_callback = function (res) {
      // console.log(res);
    }
  }
  wx.showToast({
    title: str,
    icon: 'success',
    duration: time,
    mask: true,
    success: function(res) {
      success_callback(res)
    }
  });
}
/**
 * 
 */
/**
 * get请求
 */
function get(url, data, that, success_callback, fail_callback) {
  request('get', url, data, that, success_callback, fail_callback);
}
/**
 * post请求
 */
function post(url, data, that, success_callback, fail_callback) {
  request('post', url, data, that, success_callback, fail_callback);
}
/**
 * 发起请求
 */
function request(type, url, data, that, success_callback, fail_callback) {
  if (!type) {
    type = 'get';
  }
  if (!url) {
    wx.showToast({
      title: '缺少url',
      icon: 'none',
      duration: 1000,
      mask: true
    });
    return false;
  }
  if (!data) {
    data = {};
  }
  if (!success_callback) {
    success_callback = function (res, that) {
      console.log(res);
    }
  }
  if (!fail_callback) {
    fail_callback = function (res, that) {
      console.log(res);
      wx.showToast({
        title: '网络异常！',
        icon: 'none',
        duration: 1000,
        mask: true
      });
    }
  }
  if (typeof (data.is_not_ts) === 'undefined') {
    wx.showLoading({
      title: '请稍后',
    })
  }
  wx.request({
    url: url,
    method: type,
    data: data,
    header: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      if (typeof (data.is_not_ts) === 'undefined') {
        wx.hideLoading()
      }
      success_callback(res, that)
    },
    fail: function (e) {
      if (typeof (data.is_not_ts) === 'undefined') {
        wx.hideLoading()
      }
      fail_callback(res, that)
    },
  });
}
/**
 * 读取缓存
 */
function getCache(key) {
  return wx.getStorageSync(key);
}
/**
 * 写入缓存
 */
function setCache(key, data) {
  wx.setStorageSync(key, data);
}
/**
 * 删除缓存
 */
function delCache(key) {
  wx.removeStorageSync(key);
}
/**
 * 判断类属性数量
 */
 function attrCount(obj) {
  var count = 0;
  for (var i in obj) {
    if (obj.hasOwnProperty(i)) {  // 建议加上判断,如果没有扩展对象属性可以不加
      count++;
    }
  }
  return count;
}
/**
 * 获取json长度
 */
function getJsonLength(jsonData) {
  var length = 0;
  for (var ever in jsonData) {
    length++;
  }
  return length;
}

/**
 * 解析scene
 */
function decodeScene(scene) {
  if (scene == '' || typeof (scene) == 'undefined' || scene == null) {
    return {};
  }
  var q_scene = unescape(scene)
  var q_scene_arr = q_scene.split('-')
  var q_scene_obj = {};
  for (var i = 0; i < q_scene_arr.length; i++) {
    var q_scene_item = q_scene_arr[i].split(':');
    if (q_scene_item.length >= 2) {
      q_scene_obj[q_scene_item[0]] = q_scene_item[1]
    } else {
      q_scene_obj[q_scene_item[0]] = ''
    }
  }
  return q_scene_obj;
}

module.exports = {
  getCurrentPageUrl: getCurrentPageUrl,
  getCurrentPageObj: getCurrentPageObj,
  getCurrentPageUrlWithArgs: getCurrentPageUrlWithArgs,
  get: get,
  post: post,
  msg: msg,
  success: success,
  goUrl: goUrl,
  goUrlTab: goUrlTab,
  getCache: getCache,
  setCache: setCache,
  delCache: delCache,
  attrCount: attrCount,
  getJsonLength: getJsonLength,
  goUrlClose: goUrlClose,
  decodeScene: decodeScene
}